%------------------------------------------------------------------
%------------------------------------------------------------------
%   Author: Md. Sazzad Hussain (sazzad.hussain@sydney.edu.au)
%   Learning and Affect Technologies Engineering
%   University of Sydney, 2010
%------------------------------------------------------------------
%------------------------------------------------------------------

% %%covnveter for doubleVid to matlab comma
function [status]=doubleVid_converter(subjectID,filesPath,outdir)
%Get stats from IAPS self reports
clc;
filesDir=dir(filesPath);%dir info for files path
x=1;
for m=1:length(filesDir)
    [path,name,ext,ver] = fileparts(filesDir(m).name);
    for n=1:length(subjectID(:,1))
        if strncmp(subjectID(n,:),name,27)%choose only selected subjects
            if strcmp(ext, '.txt')%.txt files
                loadAnn(x,:)=[filesPath '\' name ext];
                x=x+1;
            end
        end
    end
end
mkdir(outdir);
indexNum=0;
for j=1:(x-1)
    fid = fopen(loadAnn(j,:));%load annotation
    C = textscan(fid,'%s','delimiter', '\n');
    %file I/O for output file
    fidAT=fopen([outdir '\' subjectID(j,:) '_AutoTutor.txt'],'a');%open file
    k=1;
    while k <=length(C{1})
        if mod(k,5)==1%index
            indexNum=str2num(char(C{1}(k)));
        elseif mod(k,5)==2%time
            splitTime=regexp(C{1}(k), '-->','split');
            splitTime{1}(1);
            %annotation start time (in sec)
            splitStartT=regexp( splitTime{1}(1),':','split');
            hourStart=str2num(char(splitStartT{1}(1))); %h
            minStart=str2num(char(splitStartT{1}(2))); %m
            splitStartTSec=regexp(splitStartT{1}(3),',','split');
            secStart=str2num(char(splitStartTSec{1}(1))); %s
            msecStart=splitStartTSec{1}(2); %ms
            secStart=3600*hourStart+60*minStart+secStart;
            %annotation end time (in sec)
            splitEndT=regexp( splitTime{1}(2),':','split');
            hourEnd=str2num(char(splitEndT{1}(1))); %h
            minEnd=str2num(char(splitEndT{1}(2))); %m
            splitEndTSec=regexp(splitEndT{1}(3),',','split');
            secEnd=str2num(char(splitEndTSec{1}(1))); %s
            msecEnd=splitEndTSec{1}(2); %ms
            secEnd=3600*hourEnd+60*minEnd+secEnd;
        elseif mod(k,5)==3%category
            cat=regexprep(C{1}(k),'''','');
            cat=char(cat);
            
        elseif mod(k,5)==4%dimension
            dim=regexprep(char(C{1}{k}),'''' , '');
            dim=str2num(dim);
        elseif mod(k,5)==0%write data
            fprintf(fidAT, '%d,',indexNum);%image/seq
            fprintf(fidAT, '%d,',secStart);%image/seq
            fprintf(fidAT, '%d,',secEnd);%image/seq
            fprintf(fidAT, '%s,',cat);%image/seq
            fprintf(fidAT, '%d\n',dim);%image/seq
            indexNum=indexNum+1;
        end%AutoTutor fields
        k=k+1;
    end%file
end%subject
fclose(fid);
fclose(fidAT);
clear all;
status=1;
